//
//  WmcBrandTailorVC.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/6/9.
//

import UIKit

// MARK: - 扩展WmcImageTailorVC + WmcNavigatorProtocol

extension WmcImageTailorVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.imageTailor]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcImageTailorVM()
        if let viewModel = parms.context as? WmcImageTailorVM {
            vm = viewModel
        }
        let vc = WmcImageTailorVC(vm)
        return vc
    }
}

// MARK: - 扩展WmcImageTailorVC + WmcVMProtocol

extension WmcImageTailorVC: WmcVMProtocol {
    typealias VMType = WmcImageTailorVM
}

// MARK: - [WmcImageTailorVC]

class WmcImageTailorVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        imagePreview.configImage(viewModel.image)
        imagePreview.scrollView.isUserInteractionEnabled = false
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let imageFrame = imagePreview.imageView.convert(imagePreview.imageView.bounds, to: view)
        let margin = tailorView.hvEdge
        tailorFrameView.frame = CGRect(x: imageFrame.origin.x - margin,
                                       y: imageFrame.origin.y - margin,
                                       width: imageFrame.width + 2 * margin,
                                       height: imageFrame.height + 2 * margin)
        tailorView.frame = tailorFrameView.frame
    }

    // Internal

    lazy var saveBtn: UIButton = {
        let btn = UIButton.Wmc_themeColorBtn(color: .wmc_mainTheme, title: "保存")
        return btn
    }()
    
    lazy var imagePreview: WmcImageBrowsePreview = {
        let view = WmcImageBrowsePreview()
        return view
    }()

    lazy var tailorView: WmcImageTailorFrameView = {
        let view = WmcImageTailorFrameView()
        return view
    }()

    lazy var tailorFrameView: UIView = {
        let view = UIView()
        return view
    }()

    let minSize: CGSize = .init(width: 150, height: 150)

    override func makeUI() {
        view.backgroundColor = .black

        makeNavView(style: .dark)

        navView.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-6)
            make.width.equalTo(52.wmc_autoHor)
            make.height.equalTo(32)
            make.right.equalTo(-Wmc_standardEdge.right)
        }

        addSubview(imagePreview)

        imagePreview.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(-(Wmc_safeAreaBottom + 65))
        }

        addSubview(tailorView)
    }

    override func bindViewAction() {
        // 保存裁剪结果
        saveBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            let imageView = weakSelf.imagePreview.imageView
            let tailorView = weakSelf.tailorView
            guard let image = imageView.image else { return }
            // 计算图像比例
            let imageSize = CGSize(width: image.size.width * image.scale, height: image.size.height * image.scale)
            var croppingFrame = tailorView.convert(tailorView.bounds, to: imageView)
            croppingFrame.origin.x += tailorView.hvEdge
            croppingFrame.origin.y += tailorView.hvEdge
            croppingFrame.size.width -= 2 * tailorView.hvEdge
            croppingFrame.size.height -= 2 * tailorView.hvEdge
            let scaleX = imageSize.width / imageView.frame.width
            let scaleY = imageSize.height / imageView.frame.height
            // 裁剪新图片
            let standardRect = CGRect(x: croppingFrame.origin.x * scaleX,
                                      y: croppingFrame.origin.y * scaleY,
                                      width: croppingFrame.width * scaleX,
                                      height: croppingFrame.height * scaleY)
            // 裁剪
            guard let newCGImage = image.cgImage?.cropping(to: standardRect) else { return }
            let newImage = UIImage(cgImage: newCGImage)
            weakSelf.viewModel.croppingImageBlock?(newImage)
            weakSelf.backAction()
        }

        // 操作位置
        tailorView.touchPanBlock = { [weak self] locationType, point in
            guard let weakSelf = self else { return }
            var newFrame = weakSelf.tailorView.frame
            switch locationType {
            case .topLeft:
                newFrame.origin.x += point.x
                newFrame.origin.y += point.y
                newFrame.size.width -= point.x
                newFrame.size.height -= point.y
            case .topCenter:
                newFrame.origin.y += point.y
                newFrame.size.height -= point.y
            case .topRight:
                newFrame.origin.y += point.y
                newFrame.size.height -= point.y
                newFrame.size.width += point.x
            case .centerLeft:
                newFrame.origin.x += point.x
                newFrame.size.width -= point.x

            case .centerRight:
                newFrame.size.width += point.x
            case .bottomLeft:
                newFrame.origin.x += point.x
                newFrame.size.width -= point.x
                newFrame.size.height += point.y
            case .bottomCenter:
                newFrame.size.height += point.y
            case .bottomRight:
                newFrame.size.width += point.x
                newFrame.size.height += point.y
            }
            let tailorFrame = weakSelf.tailorFrameView.frame
            let originalFrame = weakSelf.tailorView.frame
            if newFrame.origin.x < tailorFrame.origin.x {
                newFrame.origin.x = tailorFrame.origin.x
                newFrame.size.width = originalFrame.width
            }
            if newFrame.origin.y < tailorFrame.origin.y {
                newFrame.origin.y = tailorFrame.origin.y
                newFrame.size.height = originalFrame.height
            }
            if (newFrame.origin.x + newFrame.width) > (tailorFrame.origin.x + tailorFrame.width) {
                newFrame.origin.x = originalFrame.origin.x
                newFrame.size.width = originalFrame.width
            }
            if (newFrame.origin.y + newFrame.height) > (tailorFrame.origin.y + tailorFrame.height) {
                newFrame.origin.y = originalFrame.origin.y
                newFrame.size.height = originalFrame.height
            }
            if newFrame.width < weakSelf.minSize.width {
                newFrame.origin.x = originalFrame.origin.x
                newFrame.size.width = weakSelf.minSize.width
            }
            if newFrame.height < weakSelf.minSize.height {
                newFrame.origin.y = originalFrame.origin.y
                newFrame.size.height = weakSelf.minSize.width
            }
            weakSelf.tailorView.frame = newFrame
        }
    }
}
